package com.snopy.leetcode.index1_1000.index1_100;

import com.snopy.utils.StringUtils;

import java.util.*;

/**
 * @author snopy
 * @version 1.0.0
 * @classname Question20
 * @description 有效的括号
 * 给定一个只包含‘（’,‘）’,‘{’,‘}’,‘[’,']'的字符串s,判断字符串是否有效。
 * 有效字符串需满足：
 * 1.左括号必须用相同类型的有括号闭合
 * 2.左括号必须以正确的顺序闭合
 * @email 77912204@qq.com
 * @date 2022/03/31 21:54
 */
public class Question20 {


    public static void main(String[] args) {
        String s = "[{}]";
        boolean ret =  isValid(s);
        System.out.println(ret);
    }
    public static boolean isValid(String s) {
        if (StringUtils.isEmpty(s)){
            return false;
        }
        Map<Character,Character> map = new HashMap<>();
        map.put(')','(');
        map.put(']','[');
        map.put('}','{');
        //Stack<Character> stack = new Stack<>();
        Deque<Character> stack = new LinkedList<>();

        //stack.push(s.charAt(0));
        for (int i = 0;i < s.length();i++){
            if (!stack.isEmpty()  && map.get(s.charAt(i))==stack.peek()){
                stack.pop();
            }else {
                stack.push(s.charAt(i));
            }
        }

        return stack.isEmpty();
    }
}
